const date = new Date()
const years = []
const months = []
const days = []
var Zan = require('../components/toptips/toptips');
var year = date.getFullYear(),
    month = date.getMonth();
for (let i = year; i <= (date.getFullYear() + 100); i++) {
    years.push(i)
}
for (let i = 1; i <= 12; i++) {
    if (i < 10) {
        i = '0' + i
    }
    months.push(i)
}
var app = getApp()
Page(Object.assign({}, Zan, {
    data: {
        formData: {},
        is_expirationDate: false,
        is_cvv: false,
        is_number: false,
        years: years,
        months: months,
        value: [2, 0],
        paying: false,
        adding: false
    },
    onLoad(opition) {
        let customerId = '' || opition.customerId
        let that = this
        that.setData({
                isChinese: wx.getStorageSync("isChinese"),
                totalmey: opition.totalmey,
                orderHeaderId: opition.orderHeaderId,
                value: [date.getMonth(), 0]
            })
            // this.goPay()
    },
    addCard() {
        let that = this
        let formData = that.data.formData
        if (formData['number']) {
            that.setData({
                is_number: false
            })
        } else {
            return that.setData({
                is_number: true
            })
        }
        if (formData['cvv'].length == 3 || formData['cvv'].length == 4) {
            that.setData({
                is_cvv: false
            })
        } else {
            return that.setData({
                is_cvv: true
            })
        }

        if (formData['expirationDate']) {
            that.setData({
                is_expirationDate: false
            })
        } else {
            return that.setData({
                is_expirationDate: true
            })
        }

        if (that.data.adding) {
            return;
        }
        that.setData({ adding: true })
        wx.showLoading({
            title: '',
        })
        app.getApi.getProducts('POST', {
            method: "braintree.bank.addcard",
            customerId: app.globalData.unionId || '857573744049524736', //人ID
            cvv: formData.cvv,
            number: formData.number,
            expirationDate: formData.expirationDate
        }).then((res) => {
            wx.hideLoading()
                // setTimeout(function() {
            that.setData({ adding: false })
                // })
            if (res.data.status == 'success') {
                wx.showToast({
                    title: '添加成功',
                    icon: 'success',
                    duration: 2000
                })
                that.goPay(res.data.result)
            } else {
                that.showTopTips(res.data.firstErrorMessage)
            }
            that.setData({ 'adding': false })

        }).catch(datas => {
            wx.hideLoading();
            that.setData({ adding: false })
        });
    },
    goPay(token) {
        let that = this
        let defaultCard_index = that.data.defaultCard_index
        let creditCards = that.data.creditCards
            // let pages = getCurrentPages();
            // wx.navigateBack({
            //           url:'../paydollar/paydollar?nav=addCard'
            //       });

        if (that.data.paying) {
            return;
        }
        that.setData({ paying: true })
        wx.showLoading({
            title: '',

        })
        app.getApi.getProducts('POST', {
            amount: that.data.totalmey,
            method: "braintree.bTTransaction.create.new",
            payType: 'BankCard',
            orderHeaderId: that.data.orderHeaderId,
            paymentMethodToken: token
        }).then((res) => {
            // setTimeout(function() {
            that.setData({ paying: false })
                // }, 2000)
            wx.hideLoading()
            if (res.data.isSuccess == 1) {
                wx.redirectTo({
                    url: '../paySucc/paySucc'
                })
            } else {
                that.showTopTips(res.data.firstErrorMessage)
            }
        }).catch(datas => {
            wx.hideLoading();
            that.setData({ paying: false })
        });
    },
    inputBlur(e) {
        let name = e.currentTarget.dataset.name
        let value = e.detail.value
        let that = this
        let formData = that.data.formData
        formData[name] = value
        if (formData['number']) {
            return that.setData({
                is_number: false
            })
        } else {
            return that.setData({
                is_number: true
            })
        }
        if (formData['cvv'].length == 3 || formData['cvv'].length == 4) {
            return that.setData({
                is_cvv: false
            })
        } else {
            return that.setData({
                is_cvv: true
            })
        }
        that.setData({
            [`formData.${name}`]: value
        })
    },
    inputing(e) {
        // let name = e.currentTarget.dataset.name
        // let value = e.detail.value
        // if(name=='expirationDate'){

        // }
    },
    showTopTips(tips) {
        var _this = this
        Zan.showZanTopTips(tips, {}, _this);
    },
    showpreview() {
        var animation = wx.createAnimation({
                duration: 1000,
                timingFunction: "ease",
            })
            // this.animation = animation
        animation.bottom(0).opacity(1).step()
        this.setData({
            [`animationData_1`]: animation.export()
        })
    },
    bindChange(e) {
        let _this = this
        let value = e.detail.value
        _this.setData({
            value: value
        })
    },
    citySure(e) {
        let _this = this
        let formData = _this.data.formData
        let value = _this.data.value
        let year = String(_this.data.years[value[1]])
        year = year.replace(/[0-9][0-9]/, '')
        if (_this.data.is_expirationDate) {
            _this.setData({
                is_expirationDate: false
            })
        }
        _this.setData({
            [`formData.expirationDate`]: _this.data.months[value[0]] + '/' + year
        })
        var animation = wx.createAnimation({
                duration: 1000,
                timingFunction: "ease",
            })
            // this.animation = animation
        animation.bottom(-800).opacity(0).step()
        this.setData({
            [`animationData_1`]: animation.export()
        })
    },
    cityCancel() {
        var _this = this
            // _this.setData({
            // 	value: [0, 0]
            // })
        var animation = wx.createAnimation({
                duration: 1000,
                timingFunction: "ease",
            })
            // this.animation = animation
        animation.bottom(-800).opacity(0).step()
        this.setData({
            [`animationData_1`]: animation.export()
        })
    }
}))